package com.ug.system.processor;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ug.common.cache.redis.RedisCacheService;
import com.ug.common.constant.HotConstants;
import com.ug.common.utils.SpringContextHolder;
import com.ug.system.entity.AttributeValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: LY
 * @description: B站热搜
 */
public class BibiHotHtmlPageProcessor {

    private static final Logger logger = LoggerFactory.getLogger(BibiHotHtmlPageProcessor.class);
    private static final RedisCacheService redisCacheService = SpringContextHolder.getBean(RedisCacheService.class);

    /**
     * B站热门数据爬取
     */
    public static List<AttributeValue> hotToDayProcessor(){
        logger.info("获取B站热门数据");
        String key = HotConstants.HOT_TYPE_BIBI.concat(HotConstants.HOT_TYPE_KEY);
        try {
            //  请求头（B站需要加入请求头否则获取的数据和实际热点展示的数据不一致）
            Map<String, String> head = new HashMap<>() {{
                put("Cookie", "SESSDATA=72d1da02,1750056796,29001*c2CjCsuhGlVyf991AnVYwjDSp-pdL_HgQX20xf-17fkdt1SVdc9nrvtHg9BKXJAp399_4SVmdWYmwzR0hBcks0d1JRVV96OFV5LWpibEpvdl9aSkV0eHVZN2laX0pqVFU1b1ZQTW1ETWtjTHBGVTBRWTVnOEpVdHMya3NBVWlaaUlYUDZ6dERTdmJRIIEC");
            }};
            String s = HttpRequest.get(HotConstants.BIBI_HOT_URL)
                    .headerMap(head, true)
                    .timeout(5000)
                    .execute().body();
            JSONObject jsonObject = JSONObject.parseObject(s);
            Object o = jsonObject.get("data");
            JSONObject data = JSONObject.parseObject(o.toString());
            Object o1 = data.getString("list");
            JSONArray list = JSONArray.parseArray(o1.toString());
            List<AttributeValue> attributeValues = new ArrayList<>();
            for (int i = 0; i < list.toArray().length; i++) {
                Object o2 = list.get(i);
                JSONObject jsonObject1 = JSONObject.parseObject(o2.toString());
                String title = jsonObject1.getString("title");//    热点标题
                String link = jsonObject1.getString("short_link_v2");// 热点视频链接
                Object stat = jsonObject1.getString("stat");
                JSONObject jsonObject2 = JSONObject.parseObject(stat.toString());
                String view = jsonObject2.getString("view");//  播放数
                String danmaku = jsonObject2.getString("danmaku");//    弹幕数
                String reply = jsonObject2.getString("reply");//    回复数
                String favorite = jsonObject2.getString("favorite");//  收藏数
                String coin = jsonObject2.getString("coin");//  投币数
                String share = jsonObject2.getString("share");//    分享数
                String like = jsonObject2.getString("like");//  点赞数
                double w1 = 0.1, w2 = 0.3, w3 = 0.5, w4 = 0.4, w5 = 0.6, w6 = 0.4, w7 = 0.3;
                String value = (int)Math.ceil((Integer.parseInt(view) * w1 + Integer.parseInt(danmaku) * w2 + Integer.parseInt(reply) * w3 +
                        Integer.parseInt(favorite) * w4 + Integer.parseInt(coin) * w5 + Integer.parseInt(share) * w6 + Integer.parseInt(like) * w7)) + "";
                AttributeValue attributeValue = new AttributeValue(title, link, value);
                attributeValues.add(attributeValue);
            }
            redisCacheService.put(key, attributeValues, HotConstants.HOT_TIME_OUT, TimeUnit.HOURS);
            return attributeValues;
        }catch (Exception e){
            logger.error("获取B站热门数据异常：{}", e.getMessage());
        }
        return null;
    }
}
